package sskj.regulatory.presentation.screen.authentication.video

import android.annotation.SuppressLint
import android.text.*
import android.text.style.AbsoluteSizeSpan
import android.view.View
import android.view.animation.Animation
import com.qmuiteam.qmui.util.QMUIDirection
import com.qmuiteam.qmui.util.QMUIViewHelper
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog
import com.vondear.rxtools.RxDataTool
import kotlinx.android.synthetic.main.fragment_projectdept_auth.view.*
import kotlinx.android.synthetic.main.fragment_video_auth.view.*
import sskj.regulatory.R
import sskj.regulatory.data.network.ApiConstants
import sskj.regulatory.data.response.eng.EngResponse
import sskj.regulatory.data.response.session.IndexDataResponse
import sskj.regulatory.domain.entity.user.BingDingEntity
import sskj.regulatory.presentation.adapter.ProjectDeptRecyclerArrayAdapter
import sskj.regulatory.presentation.base_mvp.base.BaseFragment
import sskj.regulatory.presentation.constans.Constans
import javax.inject.Inject

class VideoFragment : BaseFragment<VideoContract.View, VideoContract.Presenter>(), VideoContract.View {


    @Inject
    lateinit var mPresenter: VideoPresenter

    private val mAdapter: ProjectDeptRecyclerArrayAdapter = ProjectDeptRecyclerArrayAdapter(context)

    override fun injectDependencies() = activityComponent.inject(this)

    override val layoutResId: Int = R.layout.fragment_video_auth

    override fun onBackPressed(): Boolean = true

    override fun initPresenter() = mPresenter

    override fun onFetchTransitionConfig(): TransitionConfig {
        return POP_TRANSITION_CONFIG
    }

    override fun initView() {
        initHitView()
        initTopBar()
        initEvent()
    }

    private fun initTopBar() {
        mCacheView.vdTopBar.setTitle("监控视频认证")
        mCacheView.vdTopBar.setBackgroundDividerEnabled(false)

    }


    private fun initEvent() {
        mCacheView.etVdMobile.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable) {
                if (!TextUtils.isEmpty(s) && mCacheView.ivCleanVdPhone.visibility == View.GONE) {
                    mCacheView.ivCleanVdPhone.visibility = View.VISIBLE
                } else if (TextUtils.isEmpty(s)) {
                    mCacheView.ivCleanVdPhone.visibility = View.GONE
                }
            }
        })
        mCacheView.etVdPassword.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable) {
                if (!TextUtils.isEmpty(s) && mCacheView.ivCleanVdPassword.visibility == View.GONE) {
                    mCacheView.ivCleanVdPassword.visibility = View.VISIBLE
                } else if (TextUtils.isEmpty(s)) {
                    mCacheView.ivCleanVdPassword.visibility = View.GONE
                }
                if (s.isEmpty()) {
                    return
                }
            }
        })
        mCacheView.ivCleanVdPhone.setOnClickListener { mCacheView.etVdMobile.setText("") }
        mCacheView.ivCleanVdPassword.setOnClickListener { mCacheView.etVdPassword.setText("") }

        mCacheView.vdLoginBtn.setOnClickListener { mPresenter.auth(mCacheView.etVdMobile.text.toString(), mCacheView.etVdPassword.text.toString()) }
        mCacheView.vdBingdingBtn.setOnClickListener { mPresenter.bingDing(mCacheView.etVdMobile.text.toString(), mCacheView.etVdPassword.text.toString()) }
        mCacheView.vdCBingdingBtn.setOnClickListener { mPresenter.delBingDing() }
    }


    @SuppressLint("ResourceAsColor")
    private fun initHitView() {
        var ss = SpannableString("监控视频平台用户名")
        val ass = AbsoluteSizeSpan(14, true)

        ss.setSpan(ass, 0, ss.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mCacheView.etVdMobile.hint = SpannedString(ss);
        mCacheView.etVdMobile.setHintTextColor(R.color.qmui_config_color_50_pure_black)

        ss = SpannableString("密码")
        ss.setSpan(ass, 0, ss.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        mCacheView.etVdPassword.hint = SpannedString(ss);
        mCacheView.etVdPassword.setHintTextColor(R.color.qmui_config_color_50_pure_black)

        mCacheView.addressTv.text = ApiConstants.HIK_ENDPOINT

    }

    override fun initOk(bingDingEntity: BingDingEntity?) {
        when (bingDingEntity) {
            null -> {
                mCacheView.vdAuthLogin.visibility = View.VISIBLE
                mCacheView.vdAuthContent.visibility = View.GONE
                mCacheView.vdBingdingBtn.visibility = View.VISIBLE
                mCacheView.vdCBingdingBtn.visibility = View.GONE
                mCacheView.vdTopBar.removeAllRightViews()
                mCacheView.vdTopBar.removeAllLeftViews()
                mCacheView.vdTopBar.addRightTextButton("取消", R.id.topbar_right_cancel_button).setOnClickListener { popBackStack() }
            }
            else -> {
                //mPresenter.auth(mCacheView.etVdMobile.toString(), mCacheView.etVdPassword.toString())
                mCacheView.vdAuthLogin.visibility = View.GONE
                mCacheView.vdBingdingBtn.visibility = View.GONE
                mCacheView.vdCBingdingBtn.visibility = View.VISIBLE
                mCacheView.vdTopBar.removeAllRightViews()
                mCacheView.vdTopBar.removeAllLeftViews()
                mCacheView.vdTopBar.addLeftBackImageButton().setOnClickListener { popBackStack() }
                mCacheView.vdNameTv.text = bingDingEntity.userName

                mCacheView.vdTopBar.addRightTextButton("修改", R.id.topbar_right_cancel_button).setOnClickListener {
                    popBackStack()
                }
            }
        }
    }

    override fun authOk(indexData: IndexDataResponse?, username: String) {

        when (mCacheView.vdAuthLogin.visibility) {
            View.GONE -> {
                sideInContentView(indexData, username)
            }
            View.VISIBLE -> {
                QMUIViewHelper.fadeOut(mCacheView.vdAuthLogin, 300, object : Animation.AnimationListener {
                    override fun onAnimationEnd(p0: Animation?) {
                        mCacheView.vdAuthLogin.visibility = View.GONE
                        sideInContentView(indexData, username)
                    }

                    override fun onAnimationRepeat(p0: Animation?) {}
                    override fun onAnimationStart(p0: Animation?) {}
                }, true)
            }
        }
    }

    override fun bingDingOk() {

        activity.tipDialog = QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord("绑定成功")
                .create()

        activity.tipDialog?.show()

        view?.postDelayed({
            activity.tipDialog?.dismiss()

        }, 1500)
    }

    override fun delBingDingOk() {
        activity.tipDialog = QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord("解绑成功")
                .create()
        activity.tipDialog?.show()

        QMUIViewHelper.fadeIn(mCacheView.vdAuthLogin, 300, object : Animation.AnimationListener {
            override fun onAnimationEnd(p0: Animation?) {
                mCacheView.vdAuthLogin.visibility = View.VISIBLE
                sideOutContentView()
            }

            override fun onAnimationRepeat(p0: Animation?) {}
            override fun onAnimationStart(p0: Animation?) {}
        }, true)


        view?.postDelayed({
            activity.tipDialog?.dismiss()
        }, 1500)
    }


    private fun sideOutContentView() {
        QMUIViewHelper.slideOut(mCacheView.vdAuthContent, 300, object : Animation.AnimationListener {
            override fun onAnimationEnd(p0: Animation?) {
                mCacheView.vdAuthContent.visibility = View.GONE
            }

            override fun onAnimationRepeat(p0: Animation?) {}
            override fun onAnimationStart(p0: Animation?) {}
        }, true, QMUIDirection.TOP_TO_BOTTOM)
    }

    private fun sideInContentView(indexData: IndexDataResponse?, username: String) {
        QMUIViewHelper.slideIn(mCacheView.vdAuthContent, 300, object : Animation.AnimationListener {
            override fun onAnimationEnd(p0: Animation?) {
                mCacheView.vdNameTv.text = indexData?.getBingDingByRole(Constans.JK_ROLE_ID)?.userName ?: username
            }

            override fun onAnimationRepeat(p0: Animation?) {}
            override fun onAnimationStart(p0: Animation?) {
                mCacheView.vdAuthContent.visibility = View.VISIBLE
            }
        }, true, QMUIDirection.BOTTOM_TO_TOP)
    }


    override fun showLoading() {
        activity.tipDialog = QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("正在加载")
                .create()
        activity.tipDialog?.show()
    }

    override fun hideLoading() {
        activity.tipDialog?.dismiss()
    }

    override fun showTipDialogError(message: String?) {
        activity.tipDialog?.dismiss()

        activity.tipDialog = QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL)
                .setTipWord(message)
                .create()
        activity.tipDialog?.show()

        view?.postDelayed({
            activity.tipDialog?.dismiss()
        }, 1000)
    }

    override fun showError(message: String?) {
        QMUIDialog.MessageDialogBuilder(getActivity())
                .setTitle("错误提示")
                .setMessage(message)
                .addAction("确定") { dialog, _ -> dialog.dismiss() }
                .create(R.style.QMUI_Dialog).show()
    }
}